<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\policyMatching\model;

use app\cassification\model\ClassificationDictionary;
use app\Cnarea\model\Cnarea;
use plugin\saiadmin\basic\BaseNormalModel;

/**
 * 政策匹配模型
 */
class PolicyMatching extends BaseNormalModel
{

    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'matching_id';

    /**
     * 数据库表名称
     * @var string
     */
    protected $table = 'eb_policy_matching';
    protected  $append = ['city_id_text','province_id_text','district_id_text','xz_level_text']; 
    
    /**
     * 政策标题 搜索
     */
    public function searchPolicyTitleAttr($query, $value)
    {
        $query->where('policy_title', 'like', '%'.$value.'%');
    }
    public function searchApplicationStartTimeAttr($query, $value)
    {
       
        if($value){
            $query->whereBetween('application_start_time', $value);
        }
       
    }
    public function searchApplicationEndTimeAttr($query, $value)
    {
        if($value){
            $query->whereBetween('application_end_time', $value);
        }
    }
    public function getIndustryCategoryAttr($value){
        if($value){
         $value=json_decode($value,true);     
        }else{
            $value=[];
        }
        return $value;
    }
    public function setIndustryCategoryAttr($value){
       
        if($value){
         $value=json_encode($value);     
        }
        return $value;
    }
    public function getApplicationStartTimeAttr($value){
       
        if($value){
         $value=date('Y-m-d',strtotime($value));     
        }
        return $value;
    }
    public function getApplicationEndTimeAttr($value){
       
        if($value){
            $value=date('Y-m-d',strtotime($value));       
        }
        return $value;
    }
    
    public function getProvinceIdTextAttr($value,$data){
        if(isset($data['city_id'])){
            $value=Cnarea::where('area_code',$data['province_id'])->value('name');
            if(!$value)$value='全国';
        }else{
            $value='全国';
        }
        return $value;
    }
    public function getCityIdTextAttr($value,$data){
       
        if(isset($data['city_id'])){
            $value=Cnarea::where('area_code',$data['city_id'])->value('name');
            if(!$value)$value='全省';
        }else{
            $value='全省';
        }
      
        return $value;
    }
    
    public function getDistrictIdTextAttr($value,$data){
        if(isset($data['district_id'])){
            $value=Cnarea::where('area_code',$data['district_id'])->value('name');
            if(!$value)$value='全市';
        }else{
            $value='全市';
        }
        return $value;
    }
    //获取行政级别
    public function getXzLevelTextAttr($value,$data){
          if(isset($data['xz_level'])){
          return    ClassificationDictionary::where('dictionary_id', $data['xz_level'])->value('classification_name_zh');
        }
        return '';
    }


}
